import axios from 'axios'
import request from '@/router/request'

export const getJson = (url) => {
  return request({
    url: url,
    method: 'get',
    dataType: "json",
    crossDomain: true,
    cache: false
  })
}

export const getJsons = function (url) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: url,
      dataType: "json",
      crossDomain: true,
      cache: false
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

export const download = () => {
  request({
    url: `/download`,
    method: 'get',
    headers: {
      "content-disposition": "attachment;filename=total.xls",
      "content-type": "application/x-download;charset=utf-8"
    },
    responseType: "blob",
  }).then((res) => {
    let url = window.URL.createObjectURL(res)
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', '导入模板.xls')
    document.body.appendChild(link)
    link.click()
  })
}

export const downloadFile = (url, params, name) => {
  request({
    url: url,
    method: 'get',
    headers: {
      "content-disposition": "attachment;filename=total.xls",
      "content-type": "application/x-download;charset=utf-8"
    },
    responseType: "blob",
    params
  }).then((res) => {
    let url = window.URL.createObjectURL(res)
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', name)
    document.body.appendChild(link)
    link.click()
  })
}

export const downloadLocal = (url, name) => {
  const a = document.createElement('a');
  a.href = url;
  a.download = name;
  a.style.display = 'none';
  document.body.appendChild(a);
  a.click();
  a.remove();
}
